package com.ruoyi.user.controller;

import java.io.InputStream;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.user.config.AliyunOSSUtils;
import com.ruoyi.user.domain.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.user.domain.UserAdmin;
import com.ruoyi.user.service.IUserAdminService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 管理员管理Controller
 *
 * @author ruoyi
 * @date 2024-10-24
 */
@RestController
@RequestMapping("/admin")
public class UserAdminController extends BaseController
{
    @Autowired
    private IUserAdminService userAdminService;
    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;


    /**
     * 查询管理员管理列表
     */
    @RequiresPermissions("user:admin:list")
    @GetMapping("/list")
    public TableDataInfo list(UserAdmin userAdmin) {
        startPage();
        List<UserAdmin> list = userAdminService.selectUserAdminList(userAdmin);
        return getDataTable(list);
    }

    /**
     * 导出管理员管理列表
     */
    @RequiresPermissions("user:admin:export")
    @Log(title = "管理员管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserAdmin userAdmin) {
        List<UserAdmin> list = userAdminService.selectUserAdminList(userAdmin);
        ExcelUtil<UserAdmin> util = new ExcelUtil<UserAdmin>(UserAdmin.class);
        util.exportExcel(response, list, "管理员管理数据");
    }

    /**
     * 获取管理员管理详细信息
     */
    @RequiresPermissions("user:admin:query") // 管理员
    @PostMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(userAdminService.selectUserAdminById(id));
    }

    /**
     * 新增管理员管理
     */
    @RequiresPermissions("user:admin:add")
    @Log(title = "管理员管理", businessType = BusinessType.INSERT)
    @PostMapping("addAdmin")
    public AjaxResult add(@RequestBody UserAdmin userAdmin) {
        return toAjax(userAdminService.insertUserAdmin(userAdmin));
    }

    /**
     * 修改管理员管理
     */
    @RequiresPermissions("user:admin:edit")
    @Log(title = "管理员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserAdmin userAdmin) {
        return toAjax(userAdminService.updateUserAdmin(userAdmin));
    }

    /**
     * 删除管理员管理
     */
    @RequiresPermissions("user:admin:remove") // 管理员
    @Log(title = "管理员管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userAdminService.deleteUserAdminByIds(ids));
    }

    /**
     * 导入
     */
    @RequiresPermissions("user:admin:import")
    @Log(title = "导入主播管理", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(@RequestParam("file") MultipartFile file) throws IOException {
        ExcelUtil<UserAdmin> util = new ExcelUtil<UserAdmin>(UserAdmin.class);
        List<UserAdmin> userAdminList = util.importExcel(file.getInputStream());
        userAdminService.importUserAdmin(userAdminList);
        return success();
    }

//    详情
    @RequiresPermissions("user:admin:query")// 管理员
    @PostMapping("/detail/{id}") // /admin/1/detail
    public AjaxResult getIn(@PathVariable("id") Long id) {
        UserAdmin financeWithdrawals = userAdminService.selectUserAdminById(id);
        // 封装成功响应结果并返回
        return success(financeWithdrawals);
    }
    //直播内容详情
    @RequiresPermissions("user:admin:query")
    @PostMapping("/xq/{id}")
    public AjaxResult getXq(@PathVariable("id") Long id) {
        List<UserVO> userAdminList = userAdminService.xq(id);
        return success(userAdminList);
    }
    //  ------下线----------   状态更改
    @RequiresPermissions("user:admin:query")
    @PostMapping("/xx/{id}")
    public void xx(@PathVariable("id") Long id) {
         userAdminService.xx(id);
    }



    //图片上传下载
    @RequestMapping("fileUpload")
    public String upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();
        String s = aliyunOSSUtils.putObject(inputStream, originalFilename);
        return s;
    }


}
